﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class ReportCommentDao
    {
        public bool Delete(SqlConnection conn, int commentId, string userId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DELETE FROM [dbo].[Report_Comment] ");
            builder.Append("WHERE ");
            builder.Append("\t[CommentId] = @CommentId ");
            builder.Append("\tAND [UserId] = @UserId ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@CommentId", SqlDbType.Int);
            commandParameters[0].Value = commentId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = userId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, ReportCommentData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Report_Comment] ");
            builder.Append("( ");
            builder.Append("\t[ReportId], ");
            builder.Append("\t[UserId], ");
            builder.Append("\t[Content], ");
            builder.Append("\t[SendTime] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@ReportId, ");
            builder.Append("\t@UserId, ");
            builder.Append("\t@Content, ");
            builder.Append("\t@SendTime ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@ReportId", SqlDbType.Int);
            commandParameters[0].Value = data.ReportId;
            commandParameters[1] = new SqlParameter("@UserId", SqlDbType.NVarChar, 20);
            commandParameters[1].Value = data.UserId;
            commandParameters[2] = new SqlParameter("@Content", SqlDbType.NText);
            commandParameters[2].Value = data.Content;
            commandParameters[3] = new SqlParameter("@SendTime", SqlDbType.DateTime);
            commandParameters[3].Value = data.SendTime;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public bool IsExists(SqlConnection conn, int reportId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF EXISTS ");
            builder.Append("( ");
            builder.Append("\tSELECT ");
            builder.Append("\t\t[CommentId] ");
            builder.Append("\tFROM ");
            builder.Append("\t\t[dbo].[Report_Comment] ");
            builder.Append("\tWHERE ");
            builder.Append("\t\t[ReportId] = @ReportId ");
            builder.Append(") ");
            builder.Append("\tSELECT 1 ");
            builder.Append("ELSE ");
            builder.Append("\tSELECT 0 ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@ReportId", SqlDbType.Int) };
            commandParameters[0].Value = reportId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            return ((obj2 != null) && Convert.ToBoolean(obj2));
        }

        public DataTable SelectAll(SqlConnection conn, int reportId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("SELECT ");
            builder.Append("\t[Report_Comment].*, ");
            builder.Append("\t[Users].[UserName] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Report_Comment], ");
            builder.Append("\t[dbo].[Users] ");
            builder.Append("WHERE ");
            builder.Append("\t[Report_Comment].[ReportId] = @ReportId ");
            builder.Append("\tAND [Report_Comment].[UserId] = [Users].[UserId] ");
            builder.Append("ORDER BY ");
            builder.Append("\t[Report_Comment].[SendTime] DESC ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@ReportId", SqlDbType.Int) };
            commandParameters[0].Value = reportId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Report_Comment";
            return set.Tables[0];
        }
    }
}

